package com.yxw.live_vod_backend_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_backend_boot.dto.WithdrawalRecordAddDTO;
import com.yxw.live_vod_backend_boot.dto.WithdrawalRecordQueryDTO;
import com.yxw.live_vod_backend_boot.entity.YxWithdrawalRecordEntity;
import com.yxw.live_vod_backend_boot.mapper.WithdrawalRecordMapper;
import com.yxw.live_vod_backend_boot.service.WithdrawalRecordService;
import com.yxw.live_vod_backend_boot.util.PageUtils;
import com.yxw.live_vod_backend_boot.vo.WithdrawalRecordVO;
import com.yxw.live_vod_facade.feign.ShopFeignClient;
import com.yxw.live_vod_facade.vo.ShopHomePageVO;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺提现记录Service实现类
 * @author xiexuxin
 * @date 2024-01-03
 */
@Service
public class WithdrawalRecordServiceImpl extends ServiceImpl<WithdrawalRecordMapper, YxWithdrawalRecordEntity> implements WithdrawalRecordService {

    @Autowired
    private WithdrawalRecordMapper withdrawalRecordMapper;

    @Autowired
    private ShopFeignClient shopFeignClient;

    /**
     * 新增提现记录
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWithdrawalRecord(WithdrawalRecordAddDTO dto) {
        BaseResult<ShopHomePageVO> shopHomePage = shopFeignClient.getShopHomePage(dto.getShopId().toString());
        ShopHomePageVO shop = shopHomePage.getData();
        if (shop == null) {
            throw new BaseException("新增提现记录失败,该店铺不存在!");
        }
        BigDecimal balance = shop.getBalance();
        BigDecimal amount = new BigDecimal(dto.getAmount());
        if (balance.compareTo(amount) < 0) {
            throw new BaseException("添加提现记录失败,余额不足!");
        }
        YxWithdrawalRecordEntity withdrawalRecord = new YxWithdrawalRecordEntity();
        withdrawalRecord.setWithdrawTime(new Date());
        double parsedNumber = Double.parseDouble(dto.getAmount());
        String formattedNumber = String.format("%.2f", parsedNumber);
        withdrawalRecord.setAmount(formattedNumber);
        withdrawalRecord.setStatus(CommonConstant.ZERO);
        withdrawalRecord.setShopId(shop.getLiveShopId());
        withdrawalRecord.setShopName(shop.getShopName());
        if (NumberUtils.isParsable(shop.getMerchantId())){
            withdrawalRecord.setMerchantId(Long.parseLong(shop.getMerchantId()));
        }
        withdrawalRecord.setMerchantName(shop.getMerchantName());
        withdrawalRecordMapper.insert(withdrawalRecord);
    }

    /**
     * 获取提现记录详情
     * @param id
     * @return
     */
    @Override
    public WithdrawalRecordVO getWithdrawalRecordById(Long id) {
        YxWithdrawalRecordEntity withdrawalRecord = withdrawalRecordMapper.selectById(id);
        WithdrawalRecordVO vo = new WithdrawalRecordVO();
        BeanUtils.copyProperties(withdrawalRecord, vo);
        return vo;
    }

    /**
     * 获取提现记录列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<WithdrawalRecordVO> getWithdrawalRecordList(WithdrawalRecordQueryDTO dto) {
        Page<YxWithdrawalRecordEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        LambdaQueryWrapper<YxWithdrawalRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
         String shopName = StringUtils.trimToNull(dto.getShopName());
         String merchantName = StringUtils.trimToNull(dto.getMerchantName());
        queryWrapper
                .like(StringUtils.isNotBlank(shopName), YxWithdrawalRecordEntity::getShopName, shopName)
                .like(StringUtils.isNotBlank(merchantName), YxWithdrawalRecordEntity::getMerchantName, merchantName)
                .eq(dto.getStatus() != null, YxWithdrawalRecordEntity::getStatus, dto.getStatus());
        page = baseMapper.selectPage(page, queryWrapper);
        List<YxWithdrawalRecordEntity> entityList = page.getRecords();
        List<WithdrawalRecordVO> listVoList = new ArrayList<>();
        if (null != entityList && !entityList.isEmpty()) {
            listVoList = BeanUtil.copyToList(entityList, WithdrawalRecordVO.class);
        }
        return PageUtils.get(page, listVoList);
    }
}
